package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dto.AllProductCateRequest;
import com.cskaoyan.shopping.dto.AllProductCateResponse;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author LC_RSH
 * @date 2022/7/9 10:21
 */
@Slf4j
@Service
public class IProductCateServiceImpl implements IProductCateService {
    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        AllProductCateResponse response = new AllProductCateResponse();
        try {
            // 参数校验
            request.requestCheck();
            Example example = new Example(ItemCat.class);
            // 排序
            example.setOrderByClause("sort_order "+request.getSort());
            List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
            // 设置状态
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            // 设置数据
            response.setProductCateDtoList(productCateConverter.items2Dto(itemCats));
        } catch (Exception e) {
            log.error("IProductCateServiceImpl.getAllProductCate occur Exception :" + e);

            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}

/**
 * @author 86157
 * @since 2022/07/09 10:21
 */